package com.flycent.fanbao.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.HttpClient;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.R;
import com.flycent.fanbao.ui.view.CheckView;

/**
 * @author fbl
 * 本地生成验证码
 */
public class VerificationCodeActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.codeEdit ) EditText mCodeEdit;
	@ViewInject(id = R.id.color_text ) TextView mText;
	
	
	private CheckView mMyView = null;  
	private String[] res = new String[4];  //获取每次更新的验证码，可用于判断用户输入是否正确  
	private String codeStr ;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_verificationcode);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("验证码");

		mMyView = (CheckView)findViewById(R.id.checkView);  
		//初始化验证码  
		res = mMyView.getValidataAndSetImage();  
		codeStr = getCodeStr(res);
		mMyView.setOnClickListener(new OnClickListener() {  

			@Override  
			public void onClick(View v) {  
				//重新初始化验证码  
				res = mMyView.getValidataAndSetImage();           
				codeStr = getCodeStr(res);
			}  
		});  
		
		SpannableString s = new SpannableString("10 : 59 : 30");
		Pattern p = Pattern.compile("[0-9]");
		Matcher m = p.matcher(s);
		while (m.find()) {
			int start = m.start();
			int end = m.end();
			s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)),start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			s.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.black)),start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		mText.setText(s);
		
		
	}
	
	private String getCodeStr(String[] ss){
		
		StringBuilder sb = new StringBuilder();
		for(String str : ss){
		    sb.append(str);
		}
		return sb.toString();
		
	}
	
	public  void butClick(View v){
		switch (v.getId()) {
		case R.id.code_button:
			if( invalidateText(mCodeEdit)){
				App.showShortToast("请输入验证码");
				return;
			}
			if(codeStr.equals(getText(mCodeEdit))){
				App.showShortToast("验证正确");
			}else {
				App.showShortToast("验证失败");
			}
			
			break;

		default:
			break;
		}
	}
	
}
